package main;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.logging.Level;

import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;

import utils.AccentRemover;
import utils.Logger;
import action.WebSourceCollector;

public class Main {
    public static boolean isDebug = true;
    public static final String GOOGLE_URL = "http://www.google.com"; 
    
    public static void main(String[] args) {
        // Defined input param
        String keyword = "ipad";
        int numberOfPage = 3;
        int gatherDepth = 2;
        
        // Init data
        Initialize.init();
        long startTime = System.currentTimeMillis();
        String logFileName = new SimpleDateFormat("yyyy-MM-dd HHmmss").format(Calendar.getInstance().getTime());
        
        // Write log file mapping to keyword
        Logger mappingLog = new Logger("log/mapping.out", true);
        mappingLog.info("[" + keyword + "] " + logFileName, false);
        mappingLog.close();
        
        // Request and get data from google
        WebDriver driver = new HtmlUnitDriver();
        java.util.logging.Logger.getLogger("com.gargoylesoftware.htmlunit").setLevel(Level.OFF);
        java.util.logging.Logger.getLogger("org.apache.commons.httpclient").setLevel(Level.OFF);
        System.out.println("Start search by google");
        driver.get(GOOGLE_URL);
        WebElement element = driver.findElement(By.name("q"));
        element.sendKeys(keyword);
        element.submit();
        keyword = AccentRemover.removeAccent(keyword);
        
        // Create log source
        Logger log = new Logger("log/" + logFileName + ".out", false);
        log.info(keyword, false);
        Logger imageLog = new Logger("log/" + logFileName + "_image.html", false);
        imageLog.info("<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"></head><body>", false);
        imageLog.info(keyword, false);
        imageLog.info("<br>\n", false);
        Logger linkLog = new Logger("log/" + logFileName + "_link.html", false);
        linkLog.info("<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"></head><body>", false);
        linkLog.info(keyword, false);
        linkLog.info("<br>\n", false);
        
        // Process gathered data
        String source = driver.getPageSource();
        System.out.println(String.format("Analyse %s page of result in depth %s", numberOfPage, gatherDepth));
        WebSourceCollector.processResultFromGoogle(source, keyword, log, imageLog, linkLog, gatherDepth);
        for (int i = 0; i < numberOfPage - 1; i++) {
            processNextPage(driver, keyword, log, imageLog, linkLog, gatherDepth);
        }
        
        // Wait for request manager to done
        try {
            RequestManager.instance().finishOnEveryThreadDone = true;
            RequestManager.instance().join();
        } catch (InterruptedException e) {
        }
        
        // Close all log source
        imageLog.info("</body></html>", false);
        imageLog.close();
        linkLog.info("</body></html>", false);
        linkLog.close();
        log.close();
        System.out.println("Total runtime: " + (System.currentTimeMillis() - startTime));
    }
    
    private static void processNextPage(WebDriver driver, String keyword, Logger log, Logger imageLog, Logger linkLog, int gatherDepth) {
        WebElement element = getNextElement(driver);
        if (element != null) {
            String href = element.getAttribute("href");
            driver.get(href);
            String source = driver.getPageSource();
            WebSourceCollector.processResultFromGoogle(source, keyword, log, imageLog, linkLog, gatherDepth);
        } else {
            System.out.println("No more page!");
        }
    }
    
    private static WebElement getNextElement(WebDriver driver) {
        WebElement element = null;
        try {
            element = driver.findElement(By.xpath("//a[span='Tiếp'][1]"));
        } catch (NoSuchElementException ex) {
            try {
                element = driver.findElement(By.xpath("//a[span='Next'][1]"));
            } catch (NoSuchElementException e) {
            }
        }
        return element;
    }
}

